﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MVCms.Core.Developers;
using MVCms.Core.Entities;
using MVCms.Core.Services;
using Plugin.RichTextBox.Models;

namespace Plugin.RichTextBox.Controllers
{
    public class RichTextBoxController : BaseController
    {
        [PluginModule(Name = "Rich Text Box", Description = "Rich Text Box", DisplayName = "Rich Text Box")]
        [HttpGet]
        public PartialViewResult Index()
        {
            RichTextBoxModel model = new RichTextBoxModel();
            SimpleData simpleData = SimpleDataService.Get(this.CurrentPage, "RichTextBoxHtml");

            if(simpleData!=null)
            {
                model.Html = simpleData.DataValue;
            }

            return PartialView(EditMode ? "EditMode" : "ViewMode", model);
        }

        [HttpPost]
        public PartialViewResult Save(RichTextBoxModel model)
        {
            new SimpleData
                {
                    DataKey = "RichTextBoxHtml",
                    Page = this.CurrentPage,
                    DataValue = model.Html,
                }.Save();

            return PartialView(EditMode ? "EditMode" : "ViewMode", model);
        }
    }
}
